package cn.example.vip.mall.goods.controller;

import cn.example.vip.mall.util.RespResult;
import cn.example.vip.mall.goods.entity.Brand;
import cn.example.vip.mall.goods.service.BrandService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin	// 生产环境需要去掉
@RestController
@RequestMapping("/brand")
public class BrandController {
	@Autowired
	BrandService brandService;
	
	@GetMapping
	public RespResult brand() {
		return RespResult.ok();
	}
	
	@GetMapping("/category/{pid}")
	public RespResult<List<Brand>> categoryBrands(@PathVariable("pid") Integer pid) {
		List<Brand> brands = brandService.queryByCategoryId(pid);
		return RespResult.ok(brands);
	}
	
	/**
	 * 条件查询
	 * @param brand
	 * @return
	 */
	@PostMapping("/search")
	public RespResult<List<Brand>> search(@RequestBody Brand brand) {
		List<Brand> brandList = brandService.queryList(brand);
		return RespResult.ok(brandList);
	}
	
	@PostMapping("/search/{page}/{size}")
	public RespResult<List<Brand>> search(@RequestBody Brand brand,
										  @PathVariable Long page,
										  @PathVariable Long size) {
		Page<Brand> brandList = brandService.queryPageList(brand, page, size);
		return RespResult.ok(brandList);
	}
	
	/**
	 * 添加 brand
	 */
	@PostMapping("/add")
	public RespResult add(@RequestBody Brand brand) {
		brandService.save(brand);
		return RespResult.ok();
	}
	
	/**
	 * 修改 brand
	 */
	@PostMapping("/update")
	public RespResult update(@RequestBody Brand brand) {
		brandService.updateById(brand);
		return RespResult.ok();
	}
	
	/**
	 * 删除 brand
	 */
	@DeleteMapping("/{id}")
	public RespResult delete(@PathVariable("id") Integer id) {
		brandService.removeById(id);
		return RespResult.ok();
	}
	
}
